// Fills up ul#translate with Google Language Tools machine translation links
function translate() {
  function link( iso, name, title, url, current, no ) {
    var a, base = 'http://translate.google.com/translate?hl=', pos = no * -20;
    a = document.createElement( "a" );
    a.target = '_top';
    a.title = title;
    if( current ) {
      pos += "px 0px";
      a.id = 'current-lang';
      a.className = "current";
    } else {
      pos += "px 13px";
      if( iso == 'en' )
        a.href = url;
      else
        a.href = base+iso+'&ie=UTF-8&oe=UTF-8&langpair=en|'+iso+'&u='+url;
    }
    a.style.background = "url(http://ecmanaut.googlecode.com/svn/trunk/sites/ecmanaut.blogspot.com/gfx/flags.png) " + pos;
    return a;
  }
  var langs = { en:"English/English", fr:"French/Français",
		de:"German/Deutsch", it:"Italian/Italiano",
		pt:"Portuguese/Português", es:"Spanish/Español", 
		ja:"Japanese/\u65E5\u672C\u8A9E", ko:"Korean/\uD55C\uAD6D",
		"zh-CN":"Chinese/\u88AB\u7B80\u5316\u7684\u6C49\u8BED" };
  var flagbar = document.getElementById( "translate" );
  var me = location.href.replace( /.*u=([^&#]*).*/i, "$1" ), n = 0;
  var current = (/[?&]hl=([^&]+)/.exec( location.search ) || {1:"en"})[1];
  for( var i in langs ) {
    if( !langs.hasOwnProperty( i ) ) continue; // survive poisoned prototypes
    var lang = langs[i].split("/"), title = lang[0], prefix = "Translate to ";
    if( i == "en" )
      prefix = "Original ";
    else if( i == current )
      prefix = "Translated to ";
    var li = document.createElement( "li" );
    li.appendChild( link( i, lang[1], prefix + title, me, i == current, n++ ) );
    flagbar.appendChild( li );
  }
}

(window.addEventListener || function( event, cb, x ) {
  window.attachEvent( "on"+event, cb );
})( "load", translate, false );
